package com.ruoyi.starbucks.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;

@Slf4j
public class SrmService4 {
    public static String userName = "rdzzg";

    public static String password = "abc880918";

    public static String itemId = "203";

    public static String token = "";

    public static String GetPhoneNo() {
        String url = MessageFormat.format("http://180.76.248.39:9999/http.action?apiType=GetPhone&apiId={0}&apiSecret={1}&pId={2}",userName,password,itemId);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        int c = jsonObject.getInteger("c");
        if(c==1){
           JSONArray jsonArray = jsonObject.getJSONArray("data");
           if(jsonArray.size()>0){
               return jsonArray.get(0).toString();
           }
        }
        return "";
    }

    public static String GetMessage(String phoneNo) {
        String url = MessageFormat.format("http://180.76.248.39:9999/http.action?apiType=GetCode&apiId={0}&apiSecret={1}&pId={2}&pn={3}",userName,password,itemId,phoneNo);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        int c = jsonObject.getInteger("c");
        if(c==1){
            String msg = jsonObject.getString("m");
            if(StringUtils.isNotEmpty(msg)){
                return msg;
            }
            else{
                return MessageFormat.format("ERROR:尚未收到短信，请确保您已经向手机号{0}发送过短信",phoneNo);
            }
        }
        return MessageFormat.format("ERROR:尚未收到短信，请确保您已经向手机号{0}发送过短信",phoneNo);
    }

    public static String AddToBlackList(String phoneNo){
        String url =MessageFormat.format("http://180.76.248.39:9999/http.action?apiType=AddBlack&apiId={0}&apiSecret={1}&pId={2}&pn={3}",userName,password,itemId,phoneNo);
        String json = HttpUtils.sendGet(url,null);
        return "ERROR:号码拉黑失败,请再试一次";
    }

    public static void main(String[] args) throws InterruptedException {
       //String phoneNo= GetPhoneNo();
      // System.out.println(phoneNo);
        while (true){
            String Msg= GetMessage("16571224621");
            System.out.println(Msg);
            Thread.sleep(1000);
        }
    }
}
